﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolCode.Entities; 

namespace CoolCode.Web.UI.Views
{
    public class GridViewHelper
    {
        public static void HighlightStatus(GridView gridView)
        {
            gridView.RowDataBound += (sender, e) =>
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    GridView grid = (GridView)sender;
                    string status = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Status"));
                    switch (status)
                    {
                        case "I": 
                            e.Row.Cells[(int)InspectionColumn.Status].Text = "在库";
                            break;
                        case "O":
                            e.Row.BackColor = Color.YellowGreen;
                            e.Row.Cells[(int)InspectionColumn.Status].Text = "出库";
                            break;
                        case "B":
                            e.Row.BackColor = Color.Yellow;
                            e.Row.Cells[(int)InspectionColumn.Status].Text = "爆破";
                            break;
                        case "T":
                            e.Row.BackColor = Color.SkyBlue;
                            e.Row.Cells[(int)InspectionColumn.Status].Text = "待定";
                            break;
                        default:
                            e.Row.Cells[(int)InspectionColumn.Status].Text = string.Empty;
                            break;

                    }
                }
            };
        }

        
        public static void HighlightCriterion(GridView gridView)
        {
            gridView.RowDataBound += (sender, e) =>
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    var criterion = getCriterion();
                    foreach (var kv in criterion)
                    {
                        bool pass = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, kv.Key));
                        if (!pass )
                        {
                            var cell=e.Row.Cells[(int)kv.Value];
                            cell.ForeColor = Color.Red;
                            cell.Font.Bold = true;
                        }
                    }
                }
            };
        }

        private static bool loadCriterion = false;
        private static readonly  object lockObject = new object();
        private static  Dictionary<string, InspectionColumn> _criterion = new Dictionary<string, InspectionColumn>();

        private static  Dictionary<string, InspectionColumn> getCriterion()
        {
            if (!loadCriterion)
            {
                lock (lockObject)
                {
                    if (!loadCriterion)
                    {
                        Dictionary<string, InspectionColumn> criterion = new Dictionary<string, InspectionColumn>();
                        //criterion.Add("PassODFar", InspectionColumn.OutsideDiameter);
                        //criterion.Add("PassODClose", InspectionColumn.OutsideDiameter);
                        criterion.Add("PassSacculusStretch", InspectionColumn.SacculusStretch);
                        criterion.Add("PassConeHeight", InspectionColumn.ConeHeight);
                        criterion.Add("PassSacculusLength", InspectionColumn.SacculusLength);
                        //criterion.Add("PassODStressTest2", InspectionColumn.StressTest);
                        //criterion.Add("PassStressTestLength", InspectionColumn.StressTest);
                        _criterion = criterion;
                        loadCriterion = true;
                    }
                }
            }
            return _criterion;
        }
    }
}
